package com.yykj.prescription.server.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.message.center.client.dto.PrescriptionMessageBodyDTO;
import com.yykj.message.center.client.feign.MessageFeignClient;
import com.yykj.prescription.client.dto.PrescriptionCreateDTO;
import com.yykj.prescription.client.dto.PrescriptionDetailDTO;
import com.yykj.prescription.server.dto.prescription.DoctorPrescriptionListSearchDTO;
import com.yykj.prescription.server.dto.prescription.PrescriptionListSearchDTO;
import com.yykj.prescription.server.model.entity.prescription.Prescription;
import com.yykj.prescription.server.model.entity.prescription.PrescriptionNoteType;
import com.yykj.prescription.server.service.PrescriptionNoteTypeService;
import com.yykj.prescription.server.service.PrescriptionService;
import com.yykj.prescription.server.vo.PrescriptionListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "医生端-- 处方相关接口")
@RestController
@RequestMapping("/api/prescription")
public class DoctorPrescriptionController {

    @PostMapping("/create")
    @ApiOperation("处方 -- 新建")
    @ApiOperationSupport(ignoreParameters = {"westDrugList[0].id","westDrugList[0].prescriptionId"
            ,"chinaDrugList[0].id","chinaDrugList[0].prescriptionId","chinaUseInfo.prescriptionId"})
    public ResultVO<String> createPrescription(@RequestBody PrescriptionCreateDTO createDTO){
        Prescription prescription = prescriptionService.createPrescription(createDTO);
        try {
            PrescriptionMessageBodyDTO messageBodyDTO = new PrescriptionMessageBodyDTO();
            BeanUtils.copyProperties(prescription,messageBodyDTO);
            messageFeignClient.sendPrescriptionCreatedMessage(messageBodyDTO);
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
        return ResultVO.success(prescription.getPrescriptionId());
    }
    private PrescriptionService prescriptionService;

    private MessageFeignClient messageFeignClient;

    @GetMapping("/list/doctor/prescription")
    @ApiOperation("处方 -- 列表")
    public ResultVO<PageData<PrescriptionListVO>> listDoctorPrescription(DoctorPrescriptionListSearchDTO searchDTO){
        PrescriptionListSearchDTO prescriptionListSearchDTO = new PrescriptionListSearchDTO();
        BeanUtils.copyProperties(searchDTO,prescriptionListSearchDTO);
        return ResultVO.success(prescriptionService.listPrescription(prescriptionListSearchDTO));
    }

    @ApiOperation("处方 -- 明细")
    @GetMapping("/info/{prescriptionId}")
    public ResultVO<PrescriptionDetailDTO> prescriptionInfo(@PathVariable String prescriptionId){
        return ResultVO.success(prescriptionService.prescriptionInfo(prescriptionId));
    }

    @ApiOperation("处方中药处理方式 -- 列表")
    @GetMapping("/hospital/note/list/{hospitalId}")
    public ResultVO<List<PrescriptionNoteType>> hospitalNoteList(@PathVariable Integer hospitalId) {
        return ResultVO.success(prescriptionNoteTypeService.hospitalNoteList(hospitalId));
    }

    private PrescriptionNoteTypeService prescriptionNoteTypeService;

    @Autowired
    public void setPrescriptionService(PrescriptionService prescriptionService) {
        this.prescriptionService = prescriptionService;
    }

    @Resource
    public void setMessageFeignClient(MessageFeignClient messageFeignClient) {
        this.messageFeignClient = messageFeignClient;
    }

    @Autowired
    public void setPrescriptionNoteTypeService(PrescriptionNoteTypeService prescriptionNoteTypeService) {
        this.prescriptionNoteTypeService = prescriptionNoteTypeService;
    }
}
